package com.ruoyi.bussiness.service.impl;

import java.security.Security;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.bussiness.domain.SysBusinessCheckList;
import com.ruoyi.bussiness.domain.SysBusinessPolicy;
import com.ruoyi.bussiness.mapper.SysBusinessPolicyMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bussiness.mapper.SysBusinessDynamicMapper;
import com.ruoyi.bussiness.domain.SysBusinessDynamic;
import com.ruoyi.bussiness.service.ISysBusinessDynamicService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 业务-动态Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-12
 */
@Service
public class SysBusinessDynamicServiceImpl implements ISysBusinessDynamicService
{
    @Autowired
    private SysBusinessDynamicMapper sysBusinessDynamicMapper;

    @Autowired
    private SysBusinessPolicyMapper sysBusinessPolicyMapper;



    /**
     * 查询业务-动态
     *
     * @param dynamicId 业务-动态主键
     * @return 业务-动态
     */
    @Override
    public SysBusinessDynamic selectSysBusinessDynamicByDynamicId(Long dynamicId)
    {
        return sysBusinessDynamicMapper.selectSysBusinessDynamicByDynamicId(dynamicId);
    }

    /**
     * 查询业务-动态列表
     *
     * @param sysBusinessDynamic 业务-动态
     * @return 业务-动态
     */
    @Override
    public List<SysBusinessDynamic> selectSysBusinessDynamicList(SysBusinessDynamic sysBusinessDynamic)
    {
        return sysBusinessDynamicMapper.selectSysBusinessDynamicList(sysBusinessDynamic);
    }

    /**
     * 新增业务-动态
     *
     * @param sysBusinessDynamic 业务-动态
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysBusinessDynamic(SysBusinessDynamic sysBusinessDynamic)
    {
        sysBusinessDynamic.setCreateTime(DateUtils.getNowDate());
        sysBusinessDynamic.setCreateBy(SecurityUtils.getUsername());
        sysBusinessDynamic.setIsBottom("0");
        sysBusinessDynamic.setIsTop("0");
        sysBusinessDynamicMapper.insertSysBusinessDynamic(sysBusinessDynamic);

        sysBusinessDynamicMapper.updateOrderNum(sysBusinessDynamic.getDynamicId());
        return 1;
    }

    /**
     * 修改业务-动态
     *
     * @param sysBusinessDynamic 业务-动态
     * @return 结果
     */
    @Override
    public int updateSysBusinessDynamic(SysBusinessDynamic sysBusinessDynamic)
    {
        sysBusinessDynamic.setUpdateTime(DateUtils.getNowDate());
        sysBusinessDynamic.setUpdateBy(SecurityUtils.getUsername());

        return sysBusinessDynamicMapper.updateSysBusinessDynamic(sysBusinessDynamic);
    }

    /**
     * 批量删除业务-动态
     *
     * @param dynamicIds 需要删除的业务-动态主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessDynamicByDynamicIds(Long[] dynamicIds)
    {
        return sysBusinessDynamicMapper.deleteSysBusinessDynamicByDynamicIds(dynamicIds);
    }

    /**
     * 删除业务-动态信息
     *
     * @param dynamicId 业务-动态主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessDynamicByDynamicId(Long dynamicId)
    {
        return sysBusinessDynamicMapper.deleteSysBusinessDynamicByDynamicId(dynamicId);
    }

    @Override
    public int backSysBusinessDynamicByDynamicIds(Long[] dynamicIds) {
        for(Long i : dynamicIds){
//            System.out.println(i);
            SysBusinessDynamic sysBusinessDynamic = new SysBusinessDynamic();
            sysBusinessDynamic.setDynamicId(i);
            sysBusinessDynamic.setStatus("2");
            sysBusinessDynamicMapper.BackSysBusinessDynamic(sysBusinessDynamic);
        }
        return 1;
    }

    @Override
    public AjaxResult moveUpOrDown(SysBusinessDynamic entity) {
        SysBusinessDynamic moveEntity = sysBusinessDynamicMapper.selectSysBusinessDynamicByDynamicId(entity.getDynamicId());
        if(moveEntity == null){
            return AjaxResult.error("动态不存在，不能上移下移");
        }
        SysBusinessDynamic moveChangeEntity = null;
        if("up".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessDynamicMapper.getLastList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("首行数据不能上移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessDynamicMapper.updateSysBusinessDynamic(moveEntity);
            sysBusinessDynamicMapper.updateSysBusinessDynamic(moveChangeEntity);
        }else if("down".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessDynamicMapper.getNextList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("末行数据不能下移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessDynamicMapper.updateSysBusinessDynamic(moveEntity);
            sysBusinessDynamicMapper.updateSysBusinessDynamic(moveChangeEntity);
        }else{
            return AjaxResult.error("无效的移动方式");
        }
        return AjaxResult.success("移动成功",1);
    }

    @Override
    public List<Map<String, Object>> getSearch(String searchStr) {
        List<Map<String, Object>> list = new ArrayList<>();
        list = sysBusinessDynamicMapper.getSearchList(searchStr);
//        SysBusinessDynamic sysBusinessDynamic = new SysBusinessDynamic();
//        sysBusinessDynamic.setDynamicTitle(searchStr);
//        List<SysBusinessDynamic> dynamicListy= sysBusinessDynamicMapper.selectSysBusinessDynamicList(sysBusinessDynamic);
//        if(!CollectionUtils.isEmpty(dynamicListy)){
//            for(SysBusinessDynamic temp: dynamicListy){
//                Map<String, Object> tempMap = new HashMap<>();
//                tempMap.put("type","dynamic");
//                tempMap.put("title",temp.getDynamicTitle());
//                tempMap.put("date",temp.getCreateTime());
//                list.add(tempMap);
//            }
//        }
//        SysBusinessPolicy sysBusinessPolicy = new SysBusinessPolicy();
//
//        List<SysBusinessDynamic> dynamicListy=  sysBusinessPolicyMapper.selectSysBusinessPolicyList(sysBusinessPolicy)

        return list;
    }
}
